package cn.yhjz.platform.system.service.impl;

import cn.yhjz.common.Pager;
import cn.yhjz.platform.system.entity.SysRoleMenu;
import cn.yhjz.platform.system.vo.SysRoleMenuVo;
import cn.yhjz.platform.system.mapper.SysRoleMenuMapper;
import cn.yhjz.platform.system.service.ISysRoleMenuService;
import cn.hutool.core.collection.CollUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = {Exception.class})
public class SysRoleMenuServiceImpl implements ISysRoleMenuService {

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;


    @Override
    public Pager getPage(Long currentPage, Long pageSize, SysRoleMenu sysRoleMenu) {
        Pager pager = new Pager();
        Long count = sysRoleMenuMapper.getPageCount(sysRoleMenu);
        Long pageOffset = (currentPage - 1) * pageSize;
        List<SysRoleMenuVo> records = sysRoleMenuMapper.getPage(pageOffset, pageSize, sysRoleMenu);
        pager.setRows(records);
        pager.setCurrentPage(currentPage);
        pager.setPageSize(pageSize);
        pager.setTotalPageCount(count);
        return pager;
    }

    @Override
    public SysRoleMenu insert(SysRoleMenu sysRoleMenu) {
        int res = sysRoleMenuMapper.insert(sysRoleMenu);
        if (res > 0) {
            return sysRoleMenu;
        } else {
            return null;
        }
    }

    @Override
    public List<SysRoleMenuVo> select(SysRoleMenu sysRoleMenu) {
        return sysRoleMenuMapper.select(sysRoleMenu);
    }

    @Override
    public SysRoleMenuVo selectById(Long id) {
        return sysRoleMenuMapper.selectById(id);
    }

    @Override
    public int deleteById(Long id) {
        return sysRoleMenuMapper.deleteById(id);
    }

    @Override
    public int updateByIdWithNull(SysRoleMenu sysRoleMenu) {
        return sysRoleMenuMapper.updateByIdWithNull(sysRoleMenu);
    }

    @Override
    public int updateById(SysRoleMenu sysRoleMenu) {
        return sysRoleMenuMapper.updateById(sysRoleMenu);
    }

    @Override
    public int update(SysRoleMenu entity, SysRoleMenu param) {
        return sysRoleMenuMapper.update(entity, param);
    }

    @Override
    public int updateWithNull(SysRoleMenu entity, SysRoleMenu param) {
        return sysRoleMenuMapper.updateWithNull(entity, param);
    }

    @Override
    public int deleteByRoleCode(String roleCode) {
        return sysRoleMenuMapper.deleteByRoleCode(roleCode);
    }

    @Override
    public int setByRole(String roleCode, List<SysRoleMenu> sysRoleMenuList) {
        //先删除原来的所有
        sysRoleMenuMapper.deleteByRoleCode(roleCode);
        if (CollUtil.isNotEmpty(sysRoleMenuList)) {
            for (SysRoleMenu roleMenu : sysRoleMenuList) {
                sysRoleMenuMapper.insert(roleMenu);
            }
        }
        return 0;
    }
}
